import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:schoollifedemo/base/common_function.dart';
import 'package:schoollifedemo/base/url_contract.dart';
import 'package:schoollifedemo/commonwidget/button_widget.dart';
import 'package:schoollifedemo/commonwidget/text_widget_with_overflow.dart';
import 'package:schoollifedemo/commonwidget/textfield_widget.dart';

import 'circle_empty_widget.dart';

class DialogUtils {
  static void showSimpleCupertinoDialog(
    BuildContext context, {
    bool clickOutSideDismiss = false,
    String? title,
    Widget? widgets,
  }) {
    showCupertinoModalPopup(
      context: context,
      builder: (builder) {
        return SimpleDialog(
          title: title == null ? null : Text(title),
          children: [widgets ?? const SizedBox()],
        );
      },
    );
  }

  static void showPersonalInfoCupertinoDialog(
    BuildContext context, {
    bool clickOutSideDismiss = false,
    String? title,
    String? iconSrc,
    String? nickname,
    String? personalProfile,
  }) {
    showCupertinoModalPopup(
      context: context,
      builder: (builder) {
        return SimpleDialog(
          title: title == null ? null : Text(title),
          children: [
            Container(
              height: 280,
              width: 200,
              margin: const EdgeInsets.only(left: 10, right: 10),
              child: CircleEmptyWidget(
                ListView(
                  physics: const BouncingScrollPhysics(),
                  children: [
                    Center(
                      child: ClipOval(
                        child: SizedBox(
                          width: 60,
                          height: 60,
                          child: Image.network(_getIconAddress(iconSrc ?? '')),
                        ),
                      ),
                    ),
                    TextWidgetWithOverflow(
                      nickname ?? '',
                      fontSize: 16,
                      insideColor: Colors.transparent,
                    ),
                    Container(
                      margin: const EdgeInsets.only(
                          left: 10, right: 10, bottom: 10),
                      child: TextWidgetWithOverflow(
                        personalProfile ?? '',
                        fontSize: 15,
                        insideColor: Colors.white,
                        maxLine: 10,
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        );
      },
    );
  }

  static String _getIconAddress(String iconSrc) {
    return UrlContract.baseAddress + '/' + iconSrc;
  }

  static void showSimpleDialog(
    BuildContext context, {
    bool clickOutSideDismiss = false,
    String? title,
    Widget? widgets,
  }) {
    showDialog(
      context: context,
      barrierDismissible: clickOutSideDismiss,
      builder: (builder) {
        return SimpleDialog(
          title: title == null ? null : Text(title),
          children: [widgets ?? const SizedBox()],
        );
      },
    );
  }

  static void showTitleAndMessageDialog(
    BuildContext context, {
    bool clickOutSideDismiss = false,
    String? title,
    String? msg,
  }) {
    showDialog(
      context: context,
      barrierDismissible: clickOutSideDismiss,
      builder: (builder) {
        return SimpleDialog(
          title: title == null ? null : Text(title),
          children: [
            Container(
              margin: const EdgeInsets.only(
                  left: 15, right: 15, top: 5, bottom: 15),
              child: TextWidgetWithOverflow(
                msg,
                maxLine: 10,
                width: double.infinity,
                insideColor: Colors.transparent,
                textAlign: TextAlign.left,
                fontSize: 16,
              ),
            ),
          ],
        );
      },
    );
  }

  static Future<void> showEditCommitDialog(
    BuildContext context, {
    bool clickOutSideDismiss = false,
    OnTextChange? onTextChange, // 文本改变
    OnButtonClick? onBackClick, // 返回按钮
    OnButtonClick? onCancelClick, // 取消按钮
    OnButtonClick? onConfirmClick, // 确定按钮
  }) async {
    showDialog(
      context: context,
      barrierDismissible: clickOutSideDismiss,
      builder: (builder) {
        return SimpleDialog(
          children: [
            Row(
              children: [
                Container(
                  margin: const EdgeInsets.only(top: 5),
                  child: GestureDetector(
                    onTap: () {
                      if (onBackClick != null) {
                        onBackClick();
                      }
                    },
                    child: Container(
                      margin: const EdgeInsets.only(left: 15),
                      child: const Icon(Icons.arrow_back_outlined),
                    ),
                  ),
                ),
                const TextWidgetWithOverflow(
                  '你的评论',
                  textAlign: TextAlign.left,
                  fontSize: 18,
                  insideColor: Colors.transparent,
                ),
              ],
            ),
            Container(
              margin:
                  const EdgeInsets.only(left: 15, right: 15, top: 5, bottom: 5),
              child: TextFieldWidget(
                insideColor: const Color(0xFFB8DCFF),
                maxLine: 5,
                maxLength: 50,
                fontSize: 16,
                paddingLeft: 20,
                paddingRight: 20,
                paddingTop: 5,
                paddingBottom: 5,
                cursorColor: Colors.black,
                textColor: Colors.black,
                enableCountText: true,
                onTextChange: onTextChange,
                onFocus: (isFocus) {},
              ),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(
                  margin: const EdgeInsets.only(top: 5),
                  child: ButtonWidget(
                    insideWidget:
                        Image.asset('assets/images/common/icon_cancel.png'),
                    width: 80,
                    height: 50,
                    radius: 50,
                    disableColor: const Color(0x55E85252),
                    enableColor: const Color(0xFFE85252),
                    onButtonClick: onCancelClick,
                  ),
                ),
                Container(
                  margin: const EdgeInsets.only(top: 5),
                  child: ButtonWidget(
                    insideWidget:
                        Image.asset('assets/images/common/img_arrow_right.png'),
                    width: 80,
                    height: 50,
                    radius: 50,
                    onButtonClick: onConfirmClick,
                  ),
                ),
              ],
            )
          ],
        );
      },
    );
  }

  static Future<bool?> showCancelAndConfirmDialog(
    BuildContext context, {
    String? title,
    String? msg,
    bool clickOutSideDismiss = false,
  }) async {
    bool onSelect = await showDialog(
      context: context,
      barrierDismissible: clickOutSideDismiss,
      builder: (builder) {
        return SimpleDialog(
          title: title == null ? null : Text(title),
          children: [
            Column(
              children: [
                Container(
                  margin: const EdgeInsets.only(
                      left: 15, right: 15, top: 5, bottom: 15),
                  child: TextWidgetWithOverflow(
                    msg,
                    maxLine: 10,
                    width: double.infinity,
                    insideColor: Colors.transparent,
                    textAlign: TextAlign.left,
                    fontSize: 16,
                  ),
                ),
                Row(
                  children: [
                    Expanded(
                      child: GestureDetector(
                        onTap: () {
                          Navigator.of(context).pop(false);
                        },
                        child: Container(
                          margin: const EdgeInsets.only(left: 15, right: 5),
                          decoration: BoxDecoration(border: Border.all()),
                          child: const TextWidgetWithOverflow(
                            '取消',
                            fontSize: 16,
                            insideColor: Colors.transparent,
                          ),
                        ),
                      ),
                    ),
                    Expanded(
                      child: GestureDetector(
                        onTap: () {
                          Navigator.of(context).pop(true);
                        },
                        child: Container(
                          margin: const EdgeInsets.only(left: 5, right: 15),
                          decoration: BoxDecoration(border: Border.all()),
                          child: const TextWidgetWithOverflow(
                            '确定',
                            fontSize: 16,
                            insideColor: Colors.transparent,
                          ),
                        ),
                      ),
                    ),
                  ],
                )
              ],
            )
          ],
        );
      },
    );
    return onSelect;
  }
}
